package com.itheima.redbaby.activity;

import java.util.List;
import java.util.Map;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itheima.redbaby.BaseActivity;
import com.itheima.redbaby.R;
import com.itheima.redbaby.BaseActivity.MyHttpTask;
import com.itheima.redbaby.bean.Address_info;
import com.itheima.redbaby.bean.CheckAddUp;
import com.itheima.redbaby.bean.Checkout_addup;
import com.itheima.redbaby.bean.Invoice;
import com.itheima.redbaby.bean.Order;
import com.itheima.redbaby.bean.Product;
import com.itheima.redbaby.bean.ProductProperty;
import com.itheima.redbaby.engine.OrderEngine;
import com.itheima.redbaby.factory.DaoAndEngineFactory;
import com.itheima.redbaby.utils.PromptManager;

public class OrderDetailActivity extends BaseActivity implements OnClickListener {
	
	
	
	private RelativeLayout ordr_logistics_rel;
	private Intent intent;
	private TextView cancel_order_text;
	//传递过来的Order
	private Order order;
//	传递过来要取消Order位置
	private int deletePositon;
	private TextView textAdress1;
	private TextView textAdress2;
	private TextView textAdress3;
	private TextView textAdressKey4;
	private TextView textDetail1;
	private TextView textDetail2;
	private TextView textDetail3;
	private TextView textDetail4;
	private TextView textDetail5;
	private TextView textDetail6;
	private TextView textDetail7;
	private TextView textDetail8;
	private TextView textDetail9;
	private TextView textPrice2;
	private TextView textPrice3;
	private TextView textPrice4;
	private TextView textPrice5;
	private TextView textPrice6;
	private TextView textPrice7;
	private OrderDetailAdapter adapter;
	private ListView productListView;
	private List<Product> productList;
	private TextView head_back_text;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.my_order_detail_activity);
		
		initView();
		
	}

	private void initView() {
		
		intent = getIntent();
		Bundle extras = intent.getExtras();
		deletePositon = extras.getInt("position");
		order = (Order) extras.getSerializable("order");
		getData(order.getOrder_id());
		
		head_back_text = (TextView) findViewById(R.id.head_back_text);
		
		ordr_logistics_rel = (RelativeLayout) findViewById(R.id.ordr_logistics_rel);
		cancel_order_text = (TextView) findViewById(R.id.cancel_order_text);
		
		ordr_logistics_rel.setOnClickListener(this);
		cancel_order_text.setOnClickListener(this);
		head_back_text.setOnClickListener(this);
		
		//订单收货信息
		textAdress1 = (TextView) findViewById(R.id.textAdress1);
		textAdress2 = (TextView) findViewById(R.id.textAdress2);
		textAdress3 = (TextView) findViewById(R.id.textAdress3);
		textAdressKey4 = (TextView) findViewById(R.id.textAdressKey4);
		
		//订单详情
		textDetail1 = (TextView) findViewById(R.id.textDetail1);
		textDetail2 = (TextView) findViewById(R.id.textDetail2);
		textDetail3 = (TextView) findViewById(R.id.textDetail3);
		textDetail4 = (TextView) findViewById(R.id.textDetail4);
		textDetail5 = (TextView) findViewById(R.id.textDetail5);
		textDetail6 = (TextView) findViewById(R.id.textDetail6);
		textDetail7 = (TextView) findViewById(R.id.textDetail7);
		textDetail8 = (TextView) findViewById(R.id.textDetail8);
		textDetail9 = (TextView) findViewById(R.id.textDetail9);
		
		//商品金额
		textPrice2 = (TextView) findViewById(R.id.textPrice2);
		textPrice3 = (TextView) findViewById(R.id.textPrice3);
		textPrice4 = (TextView) findViewById(R.id.textPrice4);
		textPrice5 = (TextView) findViewById(R.id.textPrice5);
		textPrice6 = (TextView) findViewById(R.id.textPrice6);
		textPrice7 = (TextView) findViewById(R.id.textPrice7);
		
		productListView = (ListView) findViewById(R.id.listdetail);
		adapter = new OrderDetailAdapter();
		
		
		
	}

	private void getData(final String order_id) {
		new MyHttpTask() {
			
			@Override 
			protected Object doInBackground(Void... params) {
				OrderEngine impl = DaoAndEngineFactory.getImpl(OrderEngine.class);
				Map<String, Object> orderDetail = impl.getOrderDetail(order_id);
				
				return orderDetail;
			} 
			protected void onPostExecute(Object result) {
				
				Map<String, Object> map=(Map<String, Object>)result;
				if(map.size()>0){
					fillData(map);
				}
				
			};
			
		}.executeHttp();
		
	}

	protected void fillData(Map<String, Object> map) {
		
		productList = (List<Product>) map.get("productList");
		productListView.setAdapter(adapter);
		productListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				//TODO
				Product product = productList.get(position);
				intent=new Intent(OrderDetailActivity.this,ProductDetailActivity.class);
				intent.putExtra("pId", product.getId());
				startActivity(intent);
			}
			
		});
		
		
		Address_info address_info=(Address_info) map.get("address_info");
		textAdress1.setText(address_info.getName());
		//电话
//		textAdress2.setText(address_info.get);
		textAdress3.setText(address_info.getAddress_detail());
		//邮编
//		textAdressKey4.setText(address_info.get);
		
		Order order_info=(Order) map.get("order_info");
		
		//已取消的订单不显示取消订单按钮
		if(order_info.getType()==3){
			cancel_order_text.setVisibility(View.GONE);
		}
		
		textDetail1.setText(order_info.getStatus());
//		textDetail2.setText("");
		
		int  paytype=(Integer) map.get("payment_type");
		if(paytype==1){
			textDetail3.setText("货到付款");
		}else if(paytype==2){
			textDetail3.setText("货到POS机");
		}else if(paytype==3){
			textDetail3.setText("支付宝");
		}
		textDetail4.setText(order_info.getTime());
//		textDetail5.setText("");
		
		Invoice inv=(Invoice) map.get("invoice");
		if(inv!=null){
			textDetail6.setText("是");
			textDetail7.setText(inv.getTitle());
		}
		else{
			textDetail6.setText("否");
			textDetail7.setText("");
		}
		
		int deltype=(Integer) map.get("delivery_type");
		if(deltype==1){
			textDetail8.setText("周一至周五送货");
		}else if(deltype==2){
			textDetail8.setText("双休日及公众假期送货");
		}else if(deltype==3){
			textDetail8.setText("时间不限，工作日双休日及公众假期均可送货");
		}
		
		
		Checkout_addup check=(Checkout_addup) map.get("checkout_addup");
		textPrice2.setText(check.getTotal_price()+"元");
		textPrice3.setText(check.getProm_cut()+"元");
		textPrice4.setText(check.getFreight()+"元");
		textPrice5.setText(check.getTotal_count()+"件");
		textPrice6.setText(check.getTotal_point()+"");
		textPrice7.setText(check.getTotal_price()-check.getProm_cut()+check.getFreight()+"元");
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.ordr_logistics_rel:
			//物流信息跳转
			intent=new Intent(this,OrderLogisticsActivity.class);
			intent.putExtra("orderId", order.getOrder_id());
			startActivity(intent);
			break;
		case R.id.cancel_order_text:
			//取消订单
			cancelOrder(order.getOrder_id());
			intent=new Intent();
		
			intent.putExtra("deletePositon", deletePositon);
			setResult(100, intent);
			break;
		case R.id.head_back_text:
			finish();
		default:
			break;
		}
	}

	private void cancelOrder(final String order_id) {

		new MyHttpTask() {
			
			@Override
			protected Object doInBackground(Void... params) {
				OrderEngine impl = DaoAndEngineFactory.getImpl(OrderEngine.class);
				boolean b = impl.cancelOrder(order_id);
				
				return b;
			}
			
			protected void onPostExecute(Object result) {
			boolean b=(Boolean)result;
			if(b){
				OrderDetailActivity.this.finish();
			}else{
				PromptManager.showToast(OrderDetailActivity.this, "操作失败");
			}
			};
			
		}.executeHttp();
	}
	
	
	private class OrderDetailAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			return productList.size();
		}

		@Override
		public Object getItem(int position) {
			return productList.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if(convertView==null){
				convertView=View.inflate(OrderDetailActivity.this, R.layout.my_order_detail_listitem, null);
				holder=new ViewHolder();
				
				holder.product_name=(TextView) convertView.findViewById(R.id.order_item_prodname_text);
				holder.color=(TextView) convertView.findViewById(R.id.order_item_prodcolor_text);
				holder.size=(TextView) convertView.findViewById(R.id.order_item_prodsize_text);
				holder.count=(TextView) convertView.findViewById(R.id.order_item_prodCount_text);
				holder.price=(TextView) convertView.findViewById(R.id.order_item_subprice_text);
				
				
				convertView.setTag(holder);
			}else{
				holder=(ViewHolder) convertView.getTag();
			} 
			Product product=productList.get(position);
			List<ProductProperty> property = product.getProduct_property();
			if(property.size()>0){
				for (ProductProperty productProperty : property) {
					if("颜色".equals(productProperty.getPkey())){
						holder.color.setText(productProperty.getValue());
					}
					if("大小".equals(productProperty.getPkey())){
						holder.size.setText(productProperty.getValue());
					}
				}
			}
			
			holder.product_name.setText(product.getName());
			holder.count.setText(product.getNumber()+"件");
			holder.price.setText("￥："+product.getPrice());
			
			
			
			return convertView;
		}
		
		class ViewHolder{
			TextView product_name;
			TextView color;
			TextView size;
			TextView count;
			TextView price;
		}
	}
	
	
	
	

}
